package com.opengl.textprint.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.WorkerThread;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.opengl.textprint.R;

import java.util.concurrent.ExecutionException;

public class ImageLoad {

    public static void loadImage(ImageView view, String url) {
        Glide.with(view)
                .load(encodeUrl(url))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    @WorkerThread
    public static Bitmap getBitmapByUrl(Context context, String url) {
        try {
            return Glide.with(context)
                    .asBitmap()
                    .load(encodeUrl(url)).submit().get();
        } catch (ExecutionException | InterruptedException e) {
            Log.d("ImageLoad", "e :" + e.getMessage());
            return BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        }
    }

    public static String encodeUrl(String url) {
        Log.d("ImageLoad", "url :" + url);
        return url.replaceAll(" ", "%20");
    }
}
